package com.hung.trade.user.api;

import com.hung.trade.common.api.IUserApi;
import com.hung.trade.common.constants.TradeEnums;
import com.hung.trade.common.exception.OrderException;
import com.hung.trade.common.protocol.pay.ChangeUserMoneyReq;
import com.hung.trade.common.protocol.pay.ChangeUserMoneyResp;
import com.hung.trade.common.protocol.user.QueryUserReq;
import com.hung.trade.common.protocol.user.QueryUserResp;
import com.hung.trade.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

/**
 * @author hungki
 * @createTime 2018-10-08 下午 1:29
 */
@Controller
public class UserApiImpl implements IUserApi {
    @Autowired
    private IUserService iUserService;

    @Override
    @RequestMapping(value = "/queryUserById",method = RequestMethod.POST)
    @ResponseBody
    public QueryUserResp queryUserById(@RequestBody QueryUserReq queryUserReq) {

        QueryUserResp queryUserResp = null;
        try {
            queryUserResp =iUserService.queryUserById(queryUserReq);

        }catch (Exception e){
            e.printStackTrace();
            queryUserResp.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
            queryUserResp.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc());
        }
        return queryUserResp;
    }

    @Override
    @RequestMapping(value = "/changeUserMoney",method = RequestMethod.POST)
    @ResponseBody
    public ChangeUserMoneyResp changeUserMoney(@RequestBody ChangeUserMoneyReq changeUserMoneyReq) {
        ChangeUserMoneyResp changeUserMoneyResp = null;
        if(null == changeUserMoneyReq.getUserId() || null == changeUserMoneyReq.getUserMoney()){
            throw  new OrderException("参数不正确");
        }
        if(BigDecimal.ZERO.compareTo(changeUserMoneyReq.getUserMoney())==0){
            throw  new OrderException("金额不能为0");
        }
        try {
            changeUserMoneyResp =iUserService.changeUserMoney(changeUserMoneyReq);
        }catch (Exception e){
            e.printStackTrace();
            changeUserMoneyResp.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
            changeUserMoneyResp.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc()+":"+e.getMessage());
            throw new OrderException(e);
        }
        return changeUserMoneyResp;
    }
}
